<!-- DataList.vue -->
<template>
  <div class="data-list">
    <div class="list-header">
      <slot name="header" :total="data.length" :filtered="filteredData.length">
        共 {{ filteredData.length }} 条数据
      </slot>
    </div>
    
    <div class="list-items">
      <div 
        v-for="(item, index) in filteredData" 
        :key="item.id" 
        class="list-item"
      >
        <slot 
          name="item" 
          :item="item" 
          :index="index" 
          :is-selected="selectedItem?.id === item.id"
        >
          <div>{{ item.name }}</div>
        </slot>
      </div>
    </div>
    
    <div class="list-footer">
      <slot name="footer" :selected="selectedItem"></slot>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'

const props = defineProps({
  data: {
    type: Array,
    default: () => []
  },
  filter: {
    type: String,
    default: ''
  }
})

const selectedItem = ref(null)

const filteredData = computed(() => {
  if (!props.filter) return props.data
  return props.data.filter(item => 
    item.name.toLowerCase().includes(props.filter.toLowerCase())
  )
})

const selectItem = (item) => {
  selectedItem.value = item
}

// 暴露方法给父组件
defineExpose({
  selectItem,
  selectedItem
})
</script>



<style scoped>
.user-item {
  display: flex;
  align-items: center;
  padding: 10px;
  border: 1px solid #ddd;
  margin: 5px 0;
  cursor: pointer;
}

.user-item.selected {
  background-color: #e3f2fd;
  border-color: #2196f3;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #2196f3;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
}

.user-info {
  flex: 1;
}

.user-name {
  font-weight: bold;
}

.user-email {
  color: #666;
  font-size: 0.9em;
}
</style>
